﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace BadmintonApp.DAO.Implement
{
    public class BaseDAO
    {
        private Database _db;

        /// <summary>
        /// 默认的构造函数
        /// </summary>
        public BaseDAO()
        {
            _db = DatabaseFactory.CreateDatabase("SQLServerConnectionString");
        }

        /// <summary>
        /// 获取一个打开的数据库连接
        /// </summary>
        /// <returns>一个打开的数据库连接</returns>
        public IDbConnection GetConnection()
        {
            IDbConnection connection = _db.CreateConnection();
            connection.Open();
            return connection;
        }

        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        /// <param name="connection">数据库连接对象</param>
        public void CloseConnection(IDbConnection connection)
        {
            if (connection != null)
            {
                connection.Close();
                connection.Dispose();
            }
        }

        /// <summary>
        /// Database对象
        /// </summary>
        protected Database db
        {
            get { return _db; }
        }
    }
}
